<?php

class UserLogin extends AtomWidget
{
	public $title;

//	protected function visibilityRules()
//	{
//		return array(
//			array('allow', 'users' => array('?')),
//		);
//	}
	public function init() {
		$this->visible = Yii::app()->user->isGuest;
		parent::init();
	}

	protected function beforePrepareContent() {
		if (Yii::app()->user->getState('action') == 'register')
			$this->title = CHtml::link('Login', '/login') . ' or register';
		else
			$this->title = 'Login or '.CHtml::link('register', '/register');
	}

	protected function prepareContent()
	{
		if (Yii::app()->user->getState('action') == 'register') {
			$form = new RegisterForm;
			$fieldset = 'RegisterForm';
		} else {
			$form = new LoginForm;
			$fieldset = 'LoginForm';
		}

		if (isset($_POST[$fieldset])) {
			$form->attributes = $_POST[$fieldset];
			if ($form->validate()) {
				$this->controller->refresh();
			} else {
				return array('form' => $form);
			}
		} else return array('form' => $form);
	}
}
